package com.itic.im.core.handler;

import com.itic.im.core.ImManager;
import com.itic.im.core.exception.ImException;
import com.itic.im.core.model.ImMsg;
import com.itic.im.core.util.SoMap;

/**
 *  消息持久化默认实现
 * @author ywl
 * @version 1.0
 * @date 2022/1/19 14:54
 */
public class DefaultImMsgHandler implements ImMsgHandler {
    @Override
    public void handler(Object o) {
        if(!ImManager.getConfig().isInsertDb()) {
            return ;
        }
        // 持久化数据库
        if(null == o) {
            throw new ImException("msg content is null");
        }

        SoMap data = (SoMap) o;
        ImMsg imMsg = new ImMsg();
        imMsg.setChatType(data.getString("groupType"));
        imMsg.setSendUid(data.getString("fromUserId"));
        imMsg.setReceiveUid(data.getString("toUserId"));
        imMsg.setGroupId(data.getString("toGroupId"));
        imMsg.setMsgType(data.getString("msgType"));
        imMsg.setMsg(data.getString("msg"));
        ImManager.getImMsgRepository().saveImMsg(imMsg);
    }
}
